package com.film.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.film.dao.OrderDao;
import com.film.domain.FilmOrder;
import com.film.rowMapper.OrderRowMapper;
import com.film.util.DBUtil;

public class OrderDaoImpl implements OrderDao {
	
	private JdbcTemplate template = new JdbcTemplate(DBUtil.getDataScorse());

	@Override
	public List<FilmOrder> selecAllOrder(int uid) {
		String sql = "select * from film_order t1 " + 
						"inner join film_showings t2 " + 
						"inner join film t3 " + 
						"inner join film_seat t4 " + 
						"inner join film_office t5 " + 
						"inner join user t6 " + 
						"on t1.showings_id = t2.showings_id " + 
						"and t2.film_id = t3.film_id " + 
						"and t1.seat_id = t4.seat_id " + 
						"and t5.off_id = t2.off_id " + 
						"and t1.u_id = t6.u_id " + 
						"and t1.u_id = ? " + 
						"order by order_id asc";
		return template.query(sql,new OrderRowMapper(),uid);
	}

	@Override
	public int insertOrder(FilmOrder order) {
		String sql = "INSERT INTO film_order  VALUES (DEFAULT, ?, ?, ?, ?, ?, 0)";
		return template.update(sql,order.getOrderNum(),order.getFilmShowings().getShowingsId(),order.getUser().getuId(),order.getFilmSeat().getSeatId(),order.getCreateTime());
	}

	@Override
	public List<FilmOrder> selectOrder(String orderNum) {
		String sql = "select * from film_order t1 " + 
				"inner join film_showings t2 " + 
				"inner join film t3 " + 
				"inner join film_seat t4 " + 
				"inner join film_office t5 " + 
				"inner join user t6 " + 
				"on t1.showings_id = t2.showings_id " + 
				"and t2.film_id = t3.film_id " + 
				"and t1.seat_id = t4.seat_id " + 
				"and t5.off_id = t2.off_id " + 
				"and t1.u_id = t6.u_id " + 
				"and t1.order_num = ? " + 
				"order by t1.order_id asc";
		return template.query(sql, new OrderRowMapper() ,orderNum);
	}

	@Override
	public int removeOrder(String orderNum) {
		String sql = "delete from film_order where order_num = ?";
		return template.update(sql,orderNum);
	}

	@Override
	public int updateOrderStatus(String orderNum, int orderStatusCode) {
		String sql = "update film_order set order_flag = ? where order_num = ?";
		return template.update(sql,orderStatusCode,orderNum);
	}

	@Override
	public List<FilmOrder> selectAllOrder() {
		String sql = "SELECT * FROM film_order t1,user t2,film_showings t3,film_seat t4,film t5,film_office t6 WHERE t1.u_id = t2.u_id and t1.showings_id = t3.showings_id and t1.seat_id = t4.seat_id and t3.film_id = t5.film_id and t3.off_id = t6.off_id";
		return  template.query(sql, new OrderRowMapper());
	}

}
